﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using BCDataCenter.BOL;

public partial class WebMaster_Administrator_NhapHoaDonBanHang_Print : System.Web.UI.Page
{
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Printed();
    }

    private void Printed()
    {
        try
        {
            if (Session["MaHDPrinte"] != null)
            {
                BWInCacNghiepVuKho objNVK = new BWInCacNghiepVuKho();
                DataSet ds = new DataSet();
                //Trung Tam PP
                ds = objNVK.GetTrungTamPP(ref error, Session["Sitecode"].ToString());
                lblTrungTamPP.Text = ds.Tables[0].Rows[0]["sitename"].ToString();
                lblDiaChi.Text = ds.Tables[0].Rows[0]["Address"].ToString();
                lblTelFax.Text = ds.Tables[0].Rows[0]["TelFax"].ToString();

                BWNhapHoaDonBanHang obj = new BWNhapHoaDonBanHang();
                DataTable dtHoDon = obj.dsInfoHoaDonChuaIn(ref error, Session["MaHDPrinte"].ToString()).Tables[0]; 
                lblTenTVV.Text = dtHoDon.Rows[0]["TenTVV"].ToString();
                lblDienThoai.Text = dtHoDon.Rows[0]["DienThoai"].ToString();
                lblSoDonHang.Text = dtHoDon.Rows[0]["MaHD"].ToString();
                lblNgayDonHang.Text = dtHoDon.Rows[0]["NgayHD"].ToString();

                

                DataTable dt = obj.dsChiTietHoaDon(ref error, Session["MaHDPrinte"].ToString()).Tables[0];
                int n = dt.Rows.Count;
                int TongSoLuong = 0;
                for(int i=0; i< n; i++)
                {
                    if (dt.Rows[i]["LoaiDong"].ToString() != "")
                    {
                        if (dt.Rows[i]["ProgramType"].ToString().Substring(dt.Rows[i]["ProgramType"].ToString().Length - 2, 2) == "TT")
                        {
                            dt.Rows.RemoveAt(i);
                            dt.Rows[i]["STT"] = (i + 1).ToString();
                            i--;
                            n--;
                        }
                        else
                        {
                            TongSoLuong += int.Parse(dt.Rows[i]["SoLuong"].ToString());
                        }
                    }
                    else
                    {
                        TongSoLuong += int.Parse(dt.Rows[i]["SoLuong"].ToString());
                    }
                }
                DataRow dr = dt.NewRow();
                dr["TenHang"] = "Tổng số lượng";
                dr["SoLuong"] = TongSoLuong.ToString();
                dt.Rows.Add(dr);
                gvListChiTietHoaDon.DataSource = dt;
                gvListChiTietHoaDon.DataBind();

                gvListChiTietHoaDon.Rows[gvListChiTietHoaDon.Rows.Count - 1].Cells[3].Font.Bold = true;
                gvListChiTietHoaDon.Rows[gvListChiTietHoaDon.Rows.Count - 1].Cells[3].ForeColor = System.Drawing.Color.Red;

                PrintHelper.PrintWebControl(pnlPrintHoaDon);
            }
            
        }
        catch(Exception ex) {
            error = error + " Lỗi: " + ex;
            return; }
    }
}
